from fastapi import FastAPI

from afast_core.core_libs.redis_cache import RedisCacheFactory
from afast_core.core_libs.redis_cache.cache import cache
app = FastAPI(redoc_url=None, docs_url=None, debug=False)

@app.on_event("startup")
async def startup():
    RedisCacheFactory.init(decode_responses=False)
    isd = await RedisCacheFactory.get_async_instance().ping()
    await RedisCacheFactory.get_async_instance().set_key("nihao", '1111111111111')
    print(await RedisCacheFactory.get_async_instance().get_key("nihao"))


@app.on_event("shutdown")
async def shutdown():
    await RedisCacheFactory.close_connections()


@app.on_event("shutdown")
async def shutdown():
    await RedisCacheFactory.close_connections()


@app.get("/cached")
@cache()
async def test_route():
    print("cached")  # second time when you request this print is not working
    return 'aaaaa'


@app.get("/cached1")
@cache(namespace='zzzzzzzzzzzz')
async def test_rout1111111e():
    print("cached1111111111111111")  # second time when you request this print is not working
    return 'aaaaa'


@app.get("/cachedsunc")
@cache()
def test_rout11111111111e():
    print("cached1111111111111111")  # second time when you request this print is not working
    return 'aaaaa'
